#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 210;

int dist[N];
int g[N][N];
bool st[N];
int happy[N];
vector<int> pre[N];

vector<string> path, res_path;
int res = 0;

int n, m;
// 由于题目中的名称为字符串，需要离散化
unordered_map<string, int> mp; 
unordered_map<int, string> name;
int cnt = 0;

void Dijkstra() {
    mm(dist, INF);
    dist[0] = 0;

    for (int i = 0; i < n; i ++ ) {
        int t = -1;

        for (int j = 0; j < n; j ++ ) 
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;

        st[t] = true;

        for (int j = 0; j < n; j ++ ) {
            if (dist[j] > dist[t] + g[t][j]) {
                dist[j] = dist[t] + g[t][j];
                pre[j].clear();
                pre[j].push_back(t);
            } else if (dist[j] == dist[t] + g[t][j])
                pre[j].push_back(t);
        }
        
    }
}

// u是终点的索引, hpy是current happy value
void DFS(int u, int hpy) {
    path.push_back(name[u]);
    // 遍历到起点
    if (u == 0) {
        cnt ++ ;
        if (hpy > res) {
            res = hpy;
            res_path = path;
            reverse(res_path.begin(), res_path.end());
        } else if (hpy == res) {
            if (res / (res_path.size() - 1) < res / (path.size() - 1)) {
                res_path = path;
                reverse(res_path.begin(), res_path.end());
            }
        }
    }

    for (int i = 0; i < pre[u].size(); i ++ ) 
        DFS(pre[u][i], hpy + happy[pre[u][i]]);
    
    path.pop_back();
    return ;
}

inline void solution() {
    mm(g, INF);
    string s;
    cin >> n >> m >> s;
    mp[s] = 0;
    name[0] = s;
    for (int i = 1; i < n; i ++ ) {
        string na;
        cin >> na >> happy[i];
        mp[na] = i;
        name[i] = na;
    }

    for (int i = 0; i < m; i ++ ) {
        string start, end;
        int cost;
        cin >> start >> end >> cost;
        g[mp[start]][mp[end]] = g[mp[end]][mp[start]] = cost;
    }

    Dijkstra();
    DFS(mp["ROM"], happy[mp["ROM"]]);
    
    cout << cnt << " " << dist[mp["ROM"]] << " " << res << " " << res / (res_path.size() - 1) << endl;
    cout << res_path[0];
    for (int i = 1; i < res_path.size(); i ++ ) 
        cout << "->" << res_path[i];
    cout << endl;
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}